'use strict';
/**
 * @name: 莫凡Ctx初始化
 * @description: 初始化函数上下文
 * @version: 0.0.1
 * @author: xiech
 * @Date: 2020-12-04 17:37:08
 * @LastEditors: xiech
 * @LastEditTime: 2020-12-10 09:49:07
 */

/**
 * @description: 初始化ctx的上下文
 * @param {Object} ctx 上下文
 * @return {Object} ctx 处理后上下文
 * @author: xiech
 */

// 数据库初始化
const database = require('./database/index.js');
// 请求初始化
const request = require('./request/index.js');

// 设置平台信息 taobao 
const setplatform = async (ctx, defaultPlatform = 'taobao') => {
  ctx.mfutil.platform = defaultPlatform;
}

// 设置部署 clondapi egg local
const setdeployment = async (ctx, defaultDeployment = 'clondapi') => {
  ctx.mfutil.deployment = defaultDeployment;
}

// 设置db模式 mongo
const setdbpattern = async (ctx, defaultPattern = 'mongo') => {
  ctx.mfutil.dbpattern = defaultPattern;
}



// 初始化所有的模块
const apply = async (ctx) => {
  ctx.mfutil = {};
  // 平台处理
  await setplatform(ctx);
  await setdbpattern(ctx);
  await setdeployment(ctx);
  await database.initDB(ctx);
  await request.initRequest(ctx);
}

module.exports = {
  apply: apply,
  setPlatform:setplatform,
  setDBPattern:setdbpattern,
  setDeployment:setdeployment,
  database,
  request,
};